# -*- coding: UTF-8 -*-
import os;
import sys;
import re;
import urllib;
import urllib.request;

codeforces_head = "https://codeforces.com";
problemset = "problemset";
standing = "standing";
submission = "submissions";
userID = "";
explore_headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                        'Chrome/51.0.2704.63 Safari/537.36'}

ProID_ProName={};

def get_problem_info(expression):
    info={};
    info["name"] = "";
    info["proID"] = "";
    info["from"] = "";
    LEN = len(expression);
    Str_a_a="";
    L=-1;R=-1;
    for i in range(LEN-1,-1,-1):
        if(expression[i:i+2] == "<a"):
            L=i;
            break;
        if(expression[i:i+4] == "</a>"):
            R=i;
    #处理题目来源与题目ID
    Str_a_a=expression[L:R+1];
    pattern_from = "contest";
    re_comp = re.compile(pattern_from);
    if(len(re_comp.findall(Str_a_a)) == 1):
        info["from"] = "contest";
    else:
        info["from"] = "gym";
    pattern_proID = "[\d]+/problem/[A-Z]";
    re_comp = re.compile(pattern_proID);
    info["proID"] = (re_comp.findall(Str_a_a))[0].replace("/problem/","-");
    #print(info["proID"]);

    #处理题目名字
    pattern_name = ">.*?<";
    re_comp = re.compile(pattern_name, re.S | re.M);
    nowname = (re_comp.findall(Str_a_a))[0];

    L=0;
    R=len(nowname)-1;
    while((nowname[L] == '>') or (nowname[L] == '\r') or (nowname[L] == '\n') or (nowname[L] == ' ')):
        L += 1;
    while((nowname[R] == '<') or (nowname[R] == '\r') or (nowname[R] == '\n') or (nowname[R] == ' ')):
        R -= 1;
    nowname = nowname[L:R+1];
    info["name"] = nowname;
    return info;


def analysis_submission(pagecontext):
    submission_status = [];
    pattern_submission = '<tr data-submission-id=".*?" data-a=".*?" partyMemberIds=".*?">.*?</tr>?'
    re_comp = re.compile(pattern_submission, re.S | re.M);
    submission_status = re_comp.findall(pagecontext);
    L = len(submission_status);
    #print(submission_status);

    #这是判断题目状态的    submissionVerdict="OK"
    #这是判断题目位置的    <a href="/contest/379/problem/D">\r\n                D - New Year Letter\r\n            </a>
    for i in range(0,L):
        pattern_problem = '<a href=".*?">\r\n.*?</a>?';
        re_comp = re.compile(pattern_problem, re.S | re.M);
        problem_info = get_problem_info(re_comp.findall(submission_status[i])[0]);
        pattern_verdict = 'submissionVerdict="OK"';
        re_comp = re.compile(pattern_verdict);
        Accepted = True;
        if(len(re_comp.findall(submission_status[i])) == 0):
            Accepted = False;
        else:
            Accepted = True;
        print(Accepted);
        if(Accepted == True):
            ProID_ProName[problem_info["from"]+problem_info["proID"]] = problem_info["name"];

def get_page_range(pagecontext):
    ProID_ProName.clear();
    pattern_pagenumber = '<div class="pagination">.*?</div>?';
    re_comp = re.compile(pattern_pagenumber, re.S | re.M);
    pagerange_array = re_comp.findall(pagecontext);
    if(len(pagerange_array) == 0):return 1;
    pagerange = pagerange_array[0];
    #print(pagerange);
    pattern_pagenumber = '<li>.*?</li>?';
    re_comp = re.compile(pattern_pagenumber, re.S | re.M);
    number_array = re_comp.findall(pagerange);
    L = len(number_array);
    if(L <= 2):return 1;
    La = len(number_array[L-2]);
    Lp=-1;
    Rp=-1;
    for i in range(La-1,-1,-1):
        if(number_array[L-2][i].isdigit()==True):
            if(Rp==-1):Rp=i;
        elif((not (Rp==-1))and(Lp==-1)):
            Lp=i+1;
            break;
    #print(number_array[L-2][Lp:Rp+1]);
    return int(number_array[L-2][Lp:Rp+1]);

if(__name__ == '__main__'):
    #print("DXY _ crawler.(codeforces)");
    #print("输入用户名");
    userID = str(sys.argv[1]);
    URL_submission = codeforces_head +"/" + submission + "/" + userID;
    REQUEST_ = urllib.request.Request(url=URL_submission,headers=explore_headers);
    now_webpage = urllib.request.urlopen(REQUEST_);
    pagecontext = now_webpage.read().decode("UTF-8");
    #print(pagecontext);
    maxpage = get_page_range(pagecontext);
    for i in range(1,maxpage+1):
        now_URL_submission = codeforces_head + "/" + submission + "/" + userID + "/page/" + str(i);
        print(now_URL_submission);
        REQUEST_ = urllib.request.Request(url=now_URL_submission,headers=explore_headers);
        now_webpage = urllib.request.urlopen(REQUEST_);
        pagecontext = now_webpage.read().decode("UTF-8");
        analysis_submission(pagecontext);
    File = open("Problem_"+userID+"_codeforces.txt","w",encoding="UTF-8");
    File.write("AC:"+str(len(ProID_ProName))+"\n");
    for proid in ProID_ProName:
        File.write(str(proid)+" : "+str(ProID_ProName[proid])+"\n");
    File.close();